package com.wxj.book.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wxj.book.dao.BookMapper;
import com.wxj.book.pojo.Book;
import com.wxj.book.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookServiceImpl implements BookService {
    @Autowired
    private BookMapper bookMapper;

    /**
     * 随机抽取四本书
     * @return
     */
    @Override
    public List<Book> getRandom() {
        return bookMapper.getRandom();
    }

    /**
     * 查询图书详细信息
     * @param bookId
     * @return
     */
    @Override
    public Book getByBookId(Integer bookId) {
        return bookMapper.selectByPrimaryKey(bookId);
    }

    /**
     * 根据图书id查询图书详情
     * @param categoryId
     * @return
     */
    @Override
    public List<Book> getByCategoryId(Integer categoryId) {
        return bookMapper.getByCategoryId(categoryId);
    }

    /**
     * 查询最新到馆的四本图书
     * @return
     */
    @Override
    public List<Book> getNewBook() {

        return bookMapper.getNewBook();
    }

    /**
     * 带有分页查询
     * @param pageNum
     * @param pageSize
     * @param categoryId
     * @return
     */
    @Override
    public PageInfo<Book> getAllByCategoryId(Integer pageNum, Integer pageSize, Integer categoryId) {
        PageHelper.startPage(pageNum, pageSize);
        List<Book> books = bookMapper.getAllByCategoryId(categoryId);
        PageInfo<Book> pageInfo = new PageInfo<>(books);
        return pageInfo;
    }

    /**
     * 推荐十本书
     * @return
     */
    @Override
    public List<Book> getRecommend() {
        return bookMapper.getRecommendBook();
    }

    /**
     * 模糊查询
     * @param pageNum
     * @param pageSize
     * @param key
     * @return
     */
    @Override
    public PageInfo<Book> getByKey(Integer pageNum, Integer pageSize, String key) {
        PageHelper.startPage(pageNum, pageSize);
        List<Book> books = bookMapper.getByKey(key);
        PageInfo<Book> pageInfo = new PageInfo<>(books);
        return pageInfo;
    }
}
